package com.dgut.epidemic.controller;

import com.dgut.epidemic.mapper.UserMapper;
import com.dgut.epidemic.pojo.Daily_data;
import com.dgut.epidemic.pojo.MyUserDetails;
import com.dgut.epidemic.pojo.User;
import com.dgut.epidemic.pojo.WeChatUser;
import com.dgut.epidemic.service.UserService;
import com.dgut.epidemic.service.WeChatUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: wuhaoduan
 * Date: 2021/6/11 15:36
 */
@Controller
public class AdminMangerController {

    @Autowired
    private WeChatUserService weChatUserService;
    @Autowired
    private UserService userService;

    @GetMapping("/adminManger/message")
    public String toAdminMessage(Model model) {
        User user = userService.getUserNow();
        model.addAttribute("user",user);
        return "adminManger_message";
    }

    @PostMapping("/adminManger/update")
    public String updateAdmin(User user, Model model){
        userService.updateAdmin(user);
        model.addAttribute("user",userService.getUserNow());
        return "adminManger_message";
    }

    @GetMapping("/root/toAdd")
    public String toAdd(Model model) {
        User user = new User();
        model.addAttribute("user",user);
        model.addAttribute("roles",userService.getUserNow().getRoles());
        model.addAttribute("ERROR", "");
        model.addAttribute("MESSAGE", "");

        return "addAdmin";
    }

    @PostMapping("/root/addAdmin")
    public String addAdmin(Model model, User user){
        int flag = userService.insertUser(user);
        if(flag == 1){
            model.addAttribute("ERROR", "用户名已存在");
            model.addAttribute("MESSAGE", "");
        }
        else {
            model.addAttribute("MESSAGE", "添加成功");
            model.addAttribute("ERROR", "");
        }
        model.addAttribute("roles",userService.getUserNow().getRoles());
        return "addAdmin";
    }

    @GetMapping("/root/toShow")
    public String toShow(Model model) {
        model.addAttribute("roles",userService.getUserNow().getRoles());
        model.addAttribute("users", userService.getAllUser());
        return "showAdmin";
    }

    @PostMapping("/root/delete//{id}")
    public String del(@PathVariable("id") String id)  throws Exception{
        userService.deleteUser(id);
        return "redirect:/root/toShow";
    }

}
